package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.sky.constant.MessageConstant;
import com.sky.constant.StatusConstant;
import com.sky.context.BaseContext;
import com.sky.dto.DishDTO;
import com.sky.dto.DishPageQueryDTO;
import com.sky.entity.Dish;
import com.sky.entity.DishFlavor;
import com.sky.entity.Employee;
import com.sky.exception.DeletionNotAllowedException;
import com.sky.mapper.DishFlavorMapper;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealDishMapper;
import com.sky.result.PageResult;
import com.sky.service.DishService;
import com.sky.vo.DishVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * @version 1.0
 * @Author 郭腾高
 * @Date 2024/9/16 22:47
 */
@Service
public class DishServiceImpl implements DishService {
    @Autowired
    private DishMapper dishMapper;
    @Autowired
    private DishFlavorMapper dishFlavorMapper;
    @Autowired
    private SetmealDishMapper setmealDishMapper;
    /*新增菜品*/
    public void saveWithFlavor(DishDTO dishDTO) {
        Dish dish = new Dish();
        BeanUtils.copyProperties(dishDTO,dish);
        //向菜品表插入一条数据
        dishMapper.insert(dish);
        //在xml配置 keyProperty="id" 后可以拿到id
        //获取insert语句生成的主键值
        Long dishId = dish.getId();


        List<DishFlavor> flavors = dishDTO.getFlavors();
        if (flavors != null && flavors.size()>0){
            //遍历的同时插入口味对应的id
            flavors.forEach(dishFlavor -> {
                dishFlavor.setDishId(dishId);
            });
            //向口味表插入n条数据
            //批量插入
            dishFlavorMapper.insertBach(flavors);
        }

    }
    /*
    * 菜品分页查询
    * */
    @Override
    public PageResult pageQuary(DishPageQueryDTO dishPageQueryDTO) {
        Page<DishVO> page = dishMapper.pageQuery(dishPageQueryDTO);
        long total = page.getTotal();
        List<DishVO> result = page.getResult();
        return new PageResult(total,result);
    }
   /*
   * 批量删除菜品
   * */
    //加入事务注解
    @Transactional
    @Override
    public void deleteBatch(List<Long> ids) {
         //查询ids状态是否由启售状态，有就删除失败
        for (Long id : ids) {
          Dish dish = dishMapper.getById(id);
          if (dish.getStatus() == StatusConstant.ENABLE){
             throw new DeletionNotAllowedException(MessageConstant.DISH_ON_SALE);
          }
        }
        //查询ids对应菜品是否有关联套餐，有就删除失败
         List<Long> setmealIds = setmealDishMapper.getSetmealIdsByDishId(ids);
        if (setmealIds != null && setmealIds.size()>0){
            throw new DeletionNotAllowedException(MessageConstant.CATEGORY_BE_RELATED_BY_SETMEAL);
        }
        //成功删除对应菜品并删除菜品相关联的口味集合
              //删除菜品
         dishMapper.deleteById(ids);
              //删除关联口味
         dishFlavorMapper.deleteByDishId(ids);
    }
    /*
    *根据id查询菜品回显
    * */
    @Override
    public DishVO getById(Long id) {
        //查询id对应菜品信息
        Dish dish = dishMapper.getById(id);
        //查询id对应的口味信息
        List<DishFlavor> dishFlavor = dishFlavorMapper.getByDishId(id);

        DishVO dishVO = new DishVO();
        BeanUtils.copyProperties(dish,dishVO);
        dishVO.setFlavors(dishFlavor);
        return dishVO;
    }
    /*
    * 修改菜品
    * */
    //加入事务注解
    @Transactional
    @Override
    public void update(DishDTO dishDTO) {
        //提交修改菜品基本信息
        Dish dish = new Dish();
        BeanUtils.copyProperties(dishDTO,dish);
        dishMapper.update(dish);
        //删除口味信息
        dishFlavorMapper.deleteByDishIdOneId(dishDTO.getId());
        //插入新的口味信息
        List<DishFlavor> flavors = dishDTO.getFlavors();
        if (flavors != null && flavors.size()>0){
            //遍历的同时插入口味对应的id
            flavors.forEach(dishFlavor -> {
                dishFlavor.setDishId(dishDTO.getId());
            });
            //向口味表插入n条数据
            //批量插入
            dishFlavorMapper.insertBach(flavors);
        }


    }
    //根据分类id查询菜品
    @Override
    public List<Dish> list(Long categoryId) {
        Dish dish = Dish.builder()
                .categoryId(categoryId)
                .status(StatusConstant.ENABLE)
                .build();
        return dishMapper.list(dish);
    }
    /*设置启售停售状态*/
    @Override
    public void changeStatus(Integer status, Long id) {
        Long setmealId = setmealDishMapper.getSetmealIdsByDishIdOne(id);
        if (setmealId != null){
            throw new DeletionNotAllowedException(MessageConstant.HAVE_SETMEAL_START);
        }
        dishMapper.changeStatus(status,id);
    }
    /**
     * 条件查询菜品和口味
     * @param dish
     * @return
     */
    public List<DishVO> listWithFlavor(Dish dish) {
        List<Dish> dishList = dishMapper.list(dish);

        List<DishVO> dishVOList = new ArrayList<>();

        for (Dish d : dishList) {
            DishVO dishVO = new DishVO();
            BeanUtils.copyProperties(d,dishVO);

            //根据菜品id查询对应的口味
            List<DishFlavor> flavors = dishFlavorMapper.getByDishId(d.getId());

            dishVO.setFlavors(flavors);
            dishVOList.add(dishVO);
        }

        return dishVOList;
    }

}
